/*
 * @Description: 
 * @Version: 
 * @Autor: LiChuang
 * @Date: 2024-05-24 11:11:11
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-05-24 15:43:47
 */
function updateBadge(e) {
    let t = "";
    t = e > 9999 && e < 1e6 ? Math.trunc(e / 1e3).toString() + "k" : e > 1e6 ? "1M+" : e.toString(), chrome.action.setBadgeBackgroundColor({
        color: "#1976d2"
    }), chrome.action.setBadgeText({
        text: t
    }), chrome.action.setTitle({
        title: `Total records: ${e}`
    })
}
chrome.webRequest.onCompleted.addListener((async e => {
    console.log(e,2222)
    var t;
    if (e.url.startsWith("https://www.google.") && e.url.includes("search?tbm=map") && !e.initiator.startsWith("chrome-extension")) {
        const l = new URL(e.url);
        console.log(l,123123)
        if (l.searchParams.has("q")) {
            const n = [];
            try {
                const t = l.searchParams.get("q"),
                    r = await fetch(e.url),
                    o = await r.text(),
                    a = JSON.parse(o.slice(0, -6)),
                    s = JSON.parse(a.d.substr(4))[0][1];
                for (const e of s)
                    if (15 === e.length) {
                        const l = {
                            uuid: null,
                            query: t,
                            name: null,
                            url: null,
                            domain: null,
                            fulladdr: null,
                            addr1: null,
                            addr2: null,
                            addr3: null,
                            addr4: null,
                            district: null,
                            timezone: null,
                            reviews: null,
                            rating: null,
                            latitude: null,
                            longitude: null,
                            categories: null,
                            local_name: null,
                            local_fulladdr: null,
                            thumbnail: null,
                            phone_numbers: null,
                            created_at: null
                        };
                        if (l.uuid = e[14][78], null != e[14][7] && (l.url = e[14][7][0], l.domain = e[14][7][1]), l.fulladdr = e[14][39], e[14][4] && (l.reviews = e[14][4][8], l.rating = e[14][4][7]), l.latitude = e[14][9][2], l.longitude = e[14][9][3], l.name = e[14][11], l.categories = e[14][13], l.district = e[14][14], l.timezone = e[14][30], l.local_name = e[14][101], l.local_fulladdr = e[14][149], e[14][37][0] && (l.thumbnail = e[14][37][0][0][6][0].split("=")[0]), l.phone_numbers = e[14][178] ? e[14][178].map((e => e[3])) : null, e[14][2]) {
                            const t = e[14][2];
                            t.length >= 1 && (l.addr1 = t[0]), t.length >= 2 && (l.addr2 = t[1]), t.length >= 3 && (l.addr3 = t[2]), t.length >= 4 && (l.addr4 = t[3])
                        }
                        l.created_at = +new Date, console.log(e), console.log(l), n[l.uuid] = l
                    }
            } catch (e) {
                console.error(e)
            }
            if (Object.keys(n).length) {
                const e = undefined,
                    l = null !== (t = (await new Promise((e => chrome.storage.local.get(["found_records"], (t => e(t)))))).found_records) && void 0 !== t ? t : {},
                    r = Object.assign(l, n);
                await new Promise((e => chrome.storage.local.set({
                    found_records: r
                }, (() => e(null)))))
            }
        }
    }
}), {
    urls: ["<all_urls>"],
    types: ["xmlhttprequest"]
}, []), chrome.storage.local.get(["found_records"], (e => {
    const t = e.found_records,
        l = undefined;
    updateBadge(t ? Object.keys(t).length : 0)
})), chrome.storage.onChanged.addListener((async e => {
    if ("found_records" in e) {
        const t = undefined;
        updateBadge(e.found_records.newValue ? Object.keys(e.found_records.newValue).length : 0)
    }
})), chrome.runtime.onInstalled.addListener((e => {
    "install" !== e.reason && "update" !== e.reason, "install" === e.reason && chrome.storage.local.set({
        global: !0,
        domain: {},
        blacklist: {}
    })
}));
chrome.runtime.onInstalled.addListener(function (object) {
    if (object.reason === 'install') {
        //首次安装跳转到web站点
        chrome.tabs.create({
            url: 'https://gmplus.io/map-leads-extractor-use-guid'
        });
    }
});